//
//  SellConditionDetialController.m
//  Manager
//
//  Created by Mr.Lee on 2018/4/17.
//  Copyright © 2018年 Reabam. All rights reserved.
//

#import "SellConditionDetialController.h"
#import "LoginViewController.h"
#import "ReDataListModel.h"
#import "cashierHandsectionView.h"
#import "CashierHandOverModel.h"
#import "Global.h"
@interface SellConditionDetialController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UIView *opView;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, strong) NSString *businessTotal;
@property (nonatomic, strong) NSString *orderMoneyTotal;
@property (nonatomic, strong) NSString *refudnmoneyTotal;
@property (nonatomic, strong) NSString *wxRefudnmoneyTotal;
@property (nonatomic, strong) NSString *wxTotalAmount;
@property (nonatomic, strong) NSMutableDictionary *dict;

@property (nonatomic, strong) NSMutableArray *handOverModelArr;    // 门店实收总额
@property (nonatomic, strong) NSMutableArray *savingsListModelArr; // 储值卡消费
@property (nonatomic, strong) NSMutableArray *reDataListModelArr;  // 储值卡充值

@end

@implementation SellConditionDetialController
{
    BOOL saveAccount;
    int pageNumber;
    NSMutableArray*ModelArray;
    NSMutableArray*DataLine;
    UITableView *table;
    NSMutableArray*mItemsArray;
    UIView*tableHeaderView;
    UIView*noDataView;
    
    UIView*_dateView;
    UIButton*_dateConfirmBtn;
    UIDatePicker*_datePicker;
    UITextField *startTextField;
    UITextField *endTextField;
    NSString *StartOrEnd;
    
//    NSString *timeString;

}



#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dict = [[NSMutableDictionary alloc] init];
    _savingsListModelArr =[NSMutableArray array];
    _reDataListModelArr = [NSMutableArray array];
    _handOverModelArr = [NSMutableArray array];

//    NSDate *dat = [NSDate dateWithTimeIntervalSinceNow:0];
//    NSTimeInterval a=[dat timeIntervalSince1970]*1000;
//    timeString = [NSString stringWithFormat:@"%f", a];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.frame = CGRectMake(0, 0, ScreenWidth, 30);
    self.timeLabel = timeLabel;
    timeLabel.font = [UIFont systemFontOfSize:13.0];
    timeLabel.textColor = lanse;// [UIColor colorWithHexString:@"cccccc"];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.backgroundColor = [UIColor colorWithHexString:@"f4f4f4"];
    if ([self.beginDate isEqualToString:self.endDate]) {
        timeLabel.text = self.beginDate;
    } else{
        timeLabel.text = [NSString stringWithFormat:@"%@~%@", self.beginDate, self.endDate];
    }
    [self.view addSubview:timeLabel];
    
    [self addTable];
    
    [self requestData];
}
#pragma mark -
#pragma mark 时间处理

- (NSString *)formartWithDateString:(NSString *)date{
    NSString *newDate= [date substringToIndex:10];
    return newDate;
}
// 获取今日
- (NSString *)getCurrentTime {
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

#pragma mark -
#pragma mark talbleView
-(void)addTable{
    
    [table removeFromSuperview];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, ScreenWidth, ScreenHeight-64-30) style:UITableViewStylePlain];
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    table.delegate=self;
    table.dataSource=self;
    [self.view addSubview: table];
    
}


- (void)requestData{
    
    [self.handOverModelArr removeAllObjects];
    [self.reDataListModelArr removeAllObjects];
    [self.savingsListModelArr removeAllObjects];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [params setValue:self.companyId forKey:@"companyId"];
    
    [params setValue:self.beginDate forKey:@"beginDate"];
    [params setValue:self.endDate forKey:@"endDate"];
    NSLog(@"%@", params);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    [HttpRequest requestData:@"report/SalesSummary2" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"%@", severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                self.dict = [severDataDict.dataDict objectForKey:@"dhData"];
                
                // 门店营业总额
                NSString *businessTotal =[NSString stringWithFormat:@"%@",[[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"businessData"] objectForKey:@"businessTotal"]];
                
                // 线下订单金额
                NSString *orderMoneyTotal =[NSString stringWithFormat:@"%@",[[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"businessData"] objectForKey:@"orderMoneyTotal"]];
                // 线下退货金额
                NSString *refudnmoneyTotal =[NSString stringWithFormat:@"%@",[[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"businessData"] objectForKey:@"refudnmoneyTotal"]];
                // 商城退货金额
                NSString *wxRefudnmoneyTotal =[NSString stringWithFormat:@"%@",[[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"businessData"] objectForKey:@"wxRefudnmoneyTotal"]];
                // 商城订单金额
                NSString *wxTotalAmount =[NSString stringWithFormat:@"%@",[[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"businessData"] objectForKey:@"wxTotalAmount"]];
                self.businessTotal = businessTotal;
                self.orderMoneyTotal = orderMoneyTotal;
                self.refudnmoneyTotal = refudnmoneyTotal;
                self.wxRefudnmoneyTotal = wxRefudnmoneyTotal;
                self.wxTotalAmount = wxTotalAmount;
                
                // 门店实收总额
                NSMutableArray *dhItems = [NSMutableArray arrayWithArray:[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"dhItems"]];
                for (NSDictionary* dicc in dhItems) {
                    CashierHandOverModel *model=[CashierHandOverModel mj_objectWithKeyValues:dicc];
                    [self.handOverModelArr addObject:model];
                }
                
                // 储值卡充值
                NSMutableArray *reDataList = [NSMutableArray arrayWithArray:[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"reDataList"]];
                for (NSDictionary* dicc in reDataList) {
                    ReDataListModel *model=[ReDataListModel mj_objectWithKeyValues:dicc];
                    [self.reDataListModelArr addObject:model];
                }
                // 储值卡消费
                NSMutableArray *savingsList = [NSMutableArray arrayWithArray:[[severDataDict.dataDict objectForKey:@"dhData"] objectForKey:@"savingsList"]];
                for (NSDictionary* dicc in savingsList) {
                    CashierHandOverModel *model=[CashierHandOverModel mj_objectWithKeyValues:dicc];
                    [self.savingsListModelArr addObject:model];
                }
                
                [table reloadData];
            } else {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
            }
            
        } else {
            
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
            [MBProgressHUD showTextHudSub:subStr view:self.view];
        }
    }];
}


#pragma mark - TableView DateSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section==1){
        return 4;
    }else if (section== 2){
        return self.handOverModelArr.count;
    } else if(section == 3){
        return self.savingsListModelArr.count;;
    } else if(section == 4){
        return self.reDataListModelArr.count;
    }else{
        return 0;
    }
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  26;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"666666"];
    
    if (indexPath.section == 1) {
        
        
        if (indexPath.row == 0) {
            cell.textLabel.text = [NSString stringWithFormat:@"线下订单金额"];
            
            
            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [Global floatConvertPercentile:[self.orderMoneyTotal doubleValue]]];
        } else if (indexPath.row == 1){
            
            cell.textLabel.text = [NSString stringWithFormat:@"线下退货金额"];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [Global floatConvertPercentile:[self.refudnmoneyTotal doubleValue]]];
        } else if (indexPath.row == 2){
            
            cell.textLabel.text = [NSString stringWithFormat:@"商城订单金额"];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [Global floatConvertPercentile:[self.wxTotalAmount doubleValue]]];
        } else if (indexPath.row == 3){
            
            cell.textLabel.text = [NSString stringWithFormat:@"商城退货金额"];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [Global floatConvertPercentile:[self.wxRefudnmoneyTotal doubleValue]]];
        }
        
    } else if (indexPath.section == 2){
        CashierHandOverModel *model = self.handOverModelArr[indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@", model.payTypeName];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [Global floatConvertPercentile:[model.money doubleValue]]];
        
    } else if(indexPath.section == 3){
        
        CashierHandOverModel *model = self.savingsListModelArr[indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@", model.payTypeName];
        
        NSString *payAmount = [NSString stringWithFormat:@"%@", model.money];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %.2f", [payAmount doubleValue]];
        
    } else if(indexPath.section == 4){
        ReDataListModel *model = self.reDataListModelArr[indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@", model.payTypeName];
        
        NSString *payAmount = [NSString stringWithFormat:@"%@", model.payAmount];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %.2f", [payAmount doubleValue]];
        //        if (indexPath.row == 0) {
        //            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [[self.dict objectForKey:@"reData"] objectForKey:@"shoppingCardTotal"]];
        //        } else if (indexPath.row == 1){
        //            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥ %@", [[self.dict objectForKey:@"reData"] objectForKey:@"serviceCardTotal"]];
        //        }
    }else{
        
    }
    
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
    cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"666666"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

// 返回每行的header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    cashierHandsectionView *headerView = [cashierHandsectionView cashierHandsectionViewFromXib];
    if (section == 0) {
        headerView.frame = CGRectMake(0, 0, ScreenWidth, 50);
        UIView *lineV = [[UIView alloc] init];
        lineV.frame = CGRectMake(10, headerView.bottom-0.5, ScreenWidth-20, 0.5);
        lineV.backgroundColor = qianhuiColor;
        [headerView addSubview:lineV];
        headerView.titleLabel.text = @"订单总数";
        headerView.moneyTotal.text = [NSString stringWithFormat:@"%@", [self.dict objectForKey:@"orderCount"]];
        return headerView;
    }else if (section== 1){
        
        UIView *pView = [[UIView alloc] init];
        pView.frame = CGRectMake(0, 0, ScreenWidth, 60);
        pView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *lineV = [[UIImageView alloc] init];
        lineV.frame = CGRectMake(10, 50-0.5, ScreenWidth-20, 0.5);
        lineV.image = [UIImage imageNamed:@"hengxian"];
        [headerView addSubview:lineV];
        headerView.titleLabel.text = @"门店营业额";
        
        NSString *totalMoneyStrFomart = [Global floatConvertPercentile:self.businessTotal.doubleValue];
        headerView.moneyTotal.text = [NSString stringWithFormat:@"¥ %@", totalMoneyStrFomart];
        headerView.frame = CGRectMake(0, 0, ScreenWidth, 50);
        [pView addSubview:headerView];
        
        return pView;
        
        
    } else if (section== 2){
        UIView *pView = [[UIView alloc] init];
        pView.frame = CGRectMake(0, 0, ScreenWidth, 70);
        pView.backgroundColor = [UIColor whiteColor];
        
        
        UIImageView *lineV = [[UIImageView alloc] init];
        lineV.frame = CGRectMake(10, 50-0.5, ScreenWidth-20, 0.5);
        lineV.image = [UIImage imageNamed:@"hengxian"];
        [headerView addSubview:lineV];
        headerView.titleLabel.text = @"门店实收金额";
        
        NSString *totalMoneyStr = [self.dict objectForKey:@"moneyTotal"];
        NSString *totalMoneyStrFomart = [Global floatConvertPercentile:totalMoneyStr.doubleValue];
        headerView.moneyTotal.text = [NSString stringWithFormat:@"¥ %@", totalMoneyStrFomart];
        headerView.frame = CGRectMake(0, 10, ScreenWidth, 50);
        [pView addSubview:headerView];
        
        UIView *lineV1 = [[UIView alloc] init];
        lineV1.frame = CGRectMake(10, 10 , ScreenWidth-20, 0.5);
        lineV1.backgroundColor = qianhuiColor;
        [pView addSubview:lineV1];
        
        return pView;
        
        
    } else if(section == 3){
        UIView *pView = [[UIView alloc] init];
        pView.frame = CGRectMake(0, 0, ScreenWidth, 70);
        pView.backgroundColor = [UIColor whiteColor];
        headerView.frame = CGRectMake(0, 10, ScreenWidth, 50);
        
        UIView *lineV = [[UIView alloc] init];
        lineV.frame = CGRectMake(10, 0 , ScreenWidth-20, 0.5);
        lineV.backgroundColor = qianhuiColor;
        [headerView addSubview:lineV];
        
        UIImageView *lineV1 = [[UIImageView alloc] init];
        lineV1.frame = CGRectMake(10, 50-0.5, ScreenWidth-20, 0.5);
        lineV1.image = [UIImage imageNamed:@"hengxian"];
        [headerView addSubview:lineV1];
        
        headerView.titleLabel.text = @"储值卡消费";
        
        NSString *savingsTotal = [Global floatConvertPercentile:[[self.dict objectForKey:@"savingsTotal"] doubleValue]];
        headerView.moneyTotal.text = [NSString stringWithFormat:@"¥ %@",savingsTotal];
        
        [pView addSubview:headerView];
        return pView;
    } else if(section == 4){
        UIView *pView = [[UIView alloc] init];
        pView.frame = CGRectMake(0, 0, ScreenWidth, 70);
        pView.backgroundColor = [UIColor whiteColor];
        headerView.frame = CGRectMake(0, 10, ScreenWidth, 50);
        
        UIView *lineV = [[UIView alloc] init];
        lineV.frame = CGRectMake(10, 0 , ScreenWidth-20, 0.5);
        lineV.backgroundColor = qianhuiColor;
        [headerView addSubview:lineV];
        
        UIImageView *lineV1 = [[UIImageView alloc] init];
        lineV1.frame = CGRectMake(10, 50-0.5, ScreenWidth-20, 0.5);
        lineV1.image = [UIImage imageNamed:@"hengxian"];
        [headerView addSubview:lineV1];
        
        headerView.titleLabel.text = @"储值卡充值";
        NSString *rechargeTotal = [Global floatConvertPercentile:[[self.dict objectForKey:@"rechargeTotal"] doubleValue]];
        headerView.moneyTotal.text = [NSString stringWithFormat:@"¥ %@",rechargeTotal];
        
        [pView addSubview:headerView];
        return pView;
    }else{
        
        UIView *pView = [[UIView alloc] init];
        pView.frame = CGRectMake(0, 0, ScreenWidth, 40);
        pView.backgroundColor = [UIColor whiteColor];
        UIView *lineV = [[UIView alloc] init];
        lineV.frame = CGRectMake(10, 10 , ScreenWidth-20, 0.5);
        lineV.backgroundColor = qianhuiColor;
        [pView addSubview:lineV];
        
        return pView;
    }
    
}
// 返回每行的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 50;
    } else if (section == 1){
        return 60;
    }else if (section == 2){
        return 70;
    } else if (section == 3) {
        return 70;
    } else if (section == 4) {
        return 70;
    }else{
        return 40;
    }
}

#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


@end
